﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz"  file="RelayCommand.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace GivingAPresentation.Commons
{
    using System;
    using System.Diagnostics;
    using System.Windows.Input;

    /// <summary>
    /// A command whose Execute and CanExecute actions are relayed to methods provided by other object.
    /// </summary>
    public class RelayCommand : ICommand
    {
        /// <summary>
        /// The internal backend field for the Execute action.
        /// </summary>
        private readonly Action<object> execute;

        /// <summary>
        /// The internal backend field for the CanExecute action.
        /// </summary>
        private readonly Predicate<object> canExecute;

        /// <summary>
        /// Initializes a new instance of the RelayCommand class with the given Execute action.
        /// </summary>
        /// <param name="execute">The provided Execute action.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the RelayCommand class with the given Execute and CanExecute actions.
        /// </summary>
        /// <param name="execute">The provided Execute action.</param>
        /// <param name="canExecute">The provided CanExecute action.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            ExceptionHelper.CheckArgumentNotNull(execute, "execute");

            this.execute = execute;
            this.canExecute = canExecute;
        }

        #region ICommand Members

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should execute.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to
        /// null.</param>
        /// <returns>Returns true if this command can be executed; returns false otherwise.</returns>
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute(parameter);
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to
        /// null.</param>
        public void Execute(object parameter)
        {
            this.execute(parameter);
        }

        #endregion // ICommand Members
    }
}
